package todolist.helpers;

import java.util.ArrayList;
import java.util.List;

import todolist.structures.Task;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

public class taskDataSource {

	// Database fields
	private SQLiteDatabase database;
	private dbHelper db;

	private String[] taskColumns = { dbHelper.TASK_COLUMN_ID,
			dbHelper.TASK_COLUMN_USERID, dbHelper.TASK_COLUMN_LAT,
			dbHelper.TASK_COLUMN_LNG, dbHelper.TASK_COLUMN_DUEDATE,
			dbHelper.TASK_COLUMN_SHORTDES, dbHelper.TASK_COLUMN_LONGDES,
			dbHelper.TASK_COLUMN_TYPE, dbHelper.TASK_COLUMN_COMPLETED};
	
	public taskDataSource(Context context) {
		db = new dbHelper(context);
	}

	public void open() throws SQLException {
		database = db.getWritableDatabase();
	}

	public void close() {
		db.close();
	}
	
	public Task createTask(int userId, String lat, String lng, String dueDate, String shortDes, String longDes, String type) {
		ContentValues values = new ContentValues();
		values.put(dbHelper.TASK_COLUMN_USERID, userId);
		values.put(dbHelper.TASK_COLUMN_DUEDATE, dueDate);
		values.put(dbHelper.TASK_COLUMN_LAT, lat);
		values.put(dbHelper.TASK_COLUMN_LNG, lng);
		values.put(dbHelper.TASK_COLUMN_SHORTDES, shortDes);
		values.put(dbHelper.TASK_COLUMN_LONGDES, longDes);
		values.put(dbHelper.TASK_COLUMN_TYPE, type);
		values.put(dbHelper.TASK_COLUMN_COMPLETED, 0);
		long insertId = database.insert(dbHelper.TABLE_TASKS, null,
				values);
		// To show how to query
		Cursor cursor = database.query(dbHelper.TABLE_TASKS,
				taskColumns, dbHelper.TASK_COLUMN_ID + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		return cursorToTask(cursor);
	}
	
	public void editTask(int userId, String lat, String lng, String dueDate, String shortDes, String longDes, String type, int taskID) {
		
		// Creates a ContentValues holding all the data to be entered
		ContentValues values = new ContentValues();
		values.put(dbHelper.TASK_COLUMN_USERID, userId);
		values.put(dbHelper.TASK_COLUMN_DUEDATE, dueDate);
		values.put(dbHelper.TASK_COLUMN_LAT, lat);
		values.put(dbHelper.TASK_COLUMN_LNG, lng);
		values.put(dbHelper.TASK_COLUMN_SHORTDES, shortDes);
		values.put(dbHelper.TASK_COLUMN_LONGDES, longDes);
		values.put(dbHelper.TASK_COLUMN_TYPE, type);
		values.put(dbHelper.TASK_COLUMN_COMPLETED, 0);
		
		// Replaces the old database line
		database.update(dbHelper.TABLE_TASKS, values, "_id=" + (taskID+1), null);
	}
	
	public void completeTask(int userId, String lat, String lng, String dueDate, String shortDes, String longDes, String type, int taskID) {
		
		// Creates a ContentValues holding all the data to be entered
		ContentValues values = new ContentValues();
		values.put(dbHelper.TASK_COLUMN_USERID, userId);
		values.put(dbHelper.TASK_COLUMN_DUEDATE, dueDate);
		values.put(dbHelper.TASK_COLUMN_LAT, lat);
		values.put(dbHelper.TASK_COLUMN_LNG, lng);
		values.put(dbHelper.TASK_COLUMN_SHORTDES, shortDes);
		values.put(dbHelper.TASK_COLUMN_LONGDES, longDes);
		values.put(dbHelper.TASK_COLUMN_TYPE, type);
		values.put(dbHelper.TASK_COLUMN_COMPLETED, 1);
		
		// Replaces the old database line
		database.update(dbHelper.TABLE_TASKS, values, "_id=" + (taskID+1), null);
	}

	public void deleteTask(Task task) {
		long id = task.getId();
		System.out.println("Task deleted with id: " + id);
		database.delete(dbHelper.TABLE_TASKS, dbHelper.TASK_COLUMN_ID
				+ " = " + id, null);
	}

	
	public List<Task> getAllTasks() {
		List<Task> tasks = new ArrayList<Task>();
		Cursor cursor = database.query(dbHelper.TABLE_TASKS,
				taskColumns, null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Task task = cursorToTask(cursor);
			tasks.add(task);
			cursor.moveToNext();
		}
		
		// Make sure to close the cursor
		cursor.close();
		return tasks;
	}
	
	private Task cursorToTask(Cursor cursor) {
		Task task = new Task();
		task.setId(cursor.getLong(0));
		task.setUserId(cursor.getInt(1));
		task.setLatitude(Double.parseDouble(cursor.getString(2)));
		task.setLongitude(Double.parseDouble(cursor.getString(3)));
		task.setDate(cursor.getString(4));
		task.setShortDescription(cursor.getString(5));
		task.setLongDescription(cursor.getString(6));
		task.setTaskType(cursor.getString(7));
		task.setComplete(Integer.valueOf(cursor.getString(8)));
		return task;
	}
}